//
//  SetOpenTimeViewController.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/4/26.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit
typealias addtimeBlock = (_ model:TimeModel)->()
class SetOpenTimeViewController: BaseViewController {
    
    var block:addtimeBlock?
    lazy var openTimeView:SetOpenTimeView = {
        let view = SetOpenTimeView(dataSource: self)
        return view
    }()
    lazy var rightBtn:UIButton = {
        let btn = UIButton()
        btn.setTitle(kText("完成"), for: UIControlState.normal)
        btn.setTitleColor(kThemeColor, for: UIControlState.normal)
        btn.addTarget(self, action: #selector(rightBtnAction), for: UIControlEvents.touchUpInside)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        let size = btn.titleLabel?.sizeWithText(text: kText("完成") as NSString, font: (btn.titleLabel?.font)!, size: CGSize(width: 100, height: 44))
        btn.bounds = CGRect(x: 0, y: 0, width: (size?.width)!, height: 44)
        return btn
    }()
    var viewModel:SetOpenTimeViewModel?
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        viewModel = SetOpenTimeViewModel()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        if (viewModel?.isUpdate)! {
            viewModel?.changeModel()
        }
        
        self.title = kText("选择时间")
        view.addSubview(openTimeView)
        openTimeView.snp.makeConstraints { (make) in
            make.top.left.bottom.right.equalToSuperview()
        }
        self.creatRightItem()
    }
    
    func creatRightItem() -> Void {
        let item  = UIBarButtonItem(customView: rightBtn)
        self.navigationItem.rightBarButtonItem = item
    }
    
    @objc func rightBtnAction() -> Void {
        viewModel?.model.changeTimeStr()
        if viewModel?.model.dateArr.count == 0 {
            CYMBShow.showMessage(kText("请选择重复时间"))
            return
        }
        if block != nil {
            self.block!((viewModel?.model)!)
        }
        self.navigationController?.popViewController(animated: true)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
